package app.netant.galgame.sadbrother;

import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.db.sql.DBManager;
import cn.lily.com.db.sql.manager.DBOfflineManager;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;

/**
 * 忧伤的弟弟-爬虫入口
 * @author JiaJ
 * @date 2018年6月12日
 *
 */
public class SadBrotherAntRoot {


	private static String homeUrl = "https://www.mygalgame.com/";
	
	private static String pageUrl = "https://www.mygalgame.com/page/";
	
	public static void analyse(int maxIdx){
		
		System.out.println("整体处理开始........");
		
		//断点续传
		resume();
		
		for(int i=1;i<=maxIdx;i++){
			
			String url = "";
			if(i == 1){
				url = homeUrl;
			}else{
				url = pageUrl + i + "/";
			}
			
			System.out.println("第"+i+"页数据开始处理！");
			
			try {
				SadBrotherAntPage page = new SadBrotherAntPage(url);
				page.analyseLinkList();
			} catch (Exception e) {
				e.printStackTrace();
				getDBManager().executeUpdate("insert into ${schema}.LEO_SAD_BROTHER_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
										+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+url+"','1') ");					
				continue;
			}
			
		}
		
		System.out.println("整体处理结束........");
		
	}
	
	/**
	 * 断点续传
	 */
	public static void resume(){
		
		System.out.println("断点续传处理开始........");
		
		getDBManager().executeUpdate("delete from ${schema}.LEO_SAD_BROTHER_IMG "
				+ "where LEO_GAL_ID in "
				+ "(select LEO_GAL_ID from ${schema}.LEO_SAD_BROTHER_INFO where LEO_SITE_ANALYSE in (select EXCEP_URL from ${schema}.LEO_SAD_BROTHER_EXCEP))");
		
		getDBManager().executeUpdate("delete from ${schema}.LEO_SAD_BROTHER_INFO where LEO_SITE_ANALYSE in (select EXCEP_URL from ${schema}.LEO_SAD_BROTHER_EXCEP)");
		getDBManager().executeUpdate("delete from ${schema}.LEO_SAD_BROTHER_EXCEP");
		
		System.out.println("断点续传处理结束........");
		
	}
	
	/**
	 * 异常处理
	 */
	public static void exceptionExcute(){
		
		System.out.println("异常修复开始..........");
		
		getDBManager().executeUpdate("delete from ${schema}.LEO_SAD_BROTHER_IMG "
				+ "where LEO_GAL_ID in "
				+ "(select LEO_GAL_ID from ${schema}.LEO_SAD_BROTHER_INFO where LEO_SITE_ANALYSE in (select EXCEP_URL from ${schema}.LEO_SAD_BROTHER_EXCEP))");
		
		getDBManager().executeUpdate("delete from ${schema}.LEO_SAD_BROTHER_INFO where LEO_SITE_ANALYSE in (select EXCEP_URL from ${schema}.LEO_SAD_BROTHER_EXCEP)");

		
		//一级处理
		DataTable firstTable = getDBManager().executeQueryTable("select * from ${schema}.LEO_SAD_BROTHER_EXCEP where EXCEP_LEVEL='1'");
		getDBManager().executeUpdate("delete from ${schema}.LEO_SAD_BROTHER_EXCEP where EXCEP_LEVEL='1'");
		while(firstTable.next()){
			String url = firstTable.getString("EXCEP_URL");
			Enviroment.getEnv().getLogBean().beginLog();
			try {
				SadBrotherAntPage page = new SadBrotherAntPage(url);
				page.analyseLinkList();
			} catch (Exception e) {
				e.printStackTrace();
				getDBManager().executeUpdate("insert into ${schema}.LEO_SAD_BROTHER_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
						+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+url+"','1') ");
				continue;
			}
			Enviroment.getEnv().getLogBean().endLog();

		}
		
		//二级处理
		DataTable secondTable = getDBManager().executeQueryTable("select * from ${schema}.LEO_SAD_BROTHER_EXCEP where EXCEP_LEVEL='2'");
		getDBManager().executeUpdate("delete from ${schema}.LEO_SAD_BROTHER_EXCEP where EXCEP_LEVEL='2'");
		while(secondTable.next()){
			Enviroment.getEnv().getLogBean().beginLog();
			String url = secondTable.getString("EXCEP_URL");			
			try {
				SadBrotherAntDetail detail = new SadBrotherAntDetail(url);
				detail.analyse();
			} catch (Exception e) {
				e.printStackTrace();
				getDBManager().executeUpdate(" insert into ${schema}.LEO_SAD_BROTHER_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
						+ " values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+url+"','2') ");
				continue;
			}
			Enviroment.getEnv().getLogBean().endLog();

		}
		

		System.out.println("异常修复结束..........");
		
		
	}
	
	public static DBManager getDBManager(){
		return DBOfflineManager.getDBManager();
	}
	
	
	public static void main(String[] args) {
		
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		//全更新
		SadBrotherAntRoot.analyse(81);
		
		//异常修正
		//SadBrotherAntRoot.exceptionExcute();
		
		
	}
	
	
}
